<template>
    <div class="app-container">
        <el-form :model="userinfo" :inline="true" label-width="80px">
            <el-form-item label="预存款">
                <el-input v-model="userinfo.deposit" :disabled="disabled" />
            </el-form-item>
            <el-select v-model="queryParams.checkstatus" placeholder="选择类型">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
            <el-date-picker v-model="queryParams.startTime" type="date" placeholder="交易起始日期"> </el-date-picker>
            <el-date-picker v-model="queryParams.endTime" type="date" placeholder="交易截止日期"> </el-date-picker>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>

                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:uplog:add']">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:uplog:edit']"
        >修改</el-button>
      </el-col>-->

            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleExport" v-hasPermi="['biz:uplog:export']">导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="uplogList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="id" />
            <el-table-column label="付款时间" align="center" prop="paytime" class="widbig">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.paytime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="付款方式" align="center" prop="paytype" class="widbig" />
            <el-table-column label="付款币种类型" align="center" prop="payrbtype" class="widbig" />
            <el-table-column label="付款金额" align="center" prop="payfee" />
            <el-table-column label="银行名称" align="center" prop="bankname" />
            <el-table-column label="付款账号" align="center" prop="payaccount" />
            <el-table-column label="状态" align="center" prop="checkstatus" />
            <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:uplog:edit']">详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改充值申请对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" @close="closeDialog" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <div class="flex">
                    <el-form-item class="widbig" label="付款时间" prop="paytime">
                        <el-date-picker :disabled="disableds" clearable size="small" style="width: 200px" v-model="form.paytime" type="date" value-format="yyyy-MM-dd" placeholder="选择付款时间"> </el-date-picker>
                    </el-form-item>
                    <el-form-item class="widbig" label="付款方式" prop="paytype">
                        <el-select :disabled="disableds" v-model="form.paytype" placeholder="请选择付款方式">
                            <el-option v-for="dict in paytypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item prop="payrbtype" label="付款币种类型" class="widbig">
                        <el-select :disabled="disableds" v-model="form.payrbtype" placeholder="请选择付款币种类型">
                            <el-option v-for="dict in paycategoryOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="付款金额" prop="payfee">
                        <el-input :disabled="disableds" v-model="form.payfee" placeholder="请输入付款金额" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="10" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="银行名称" prop="bankname">
                        <el-input :disabled="disableds" v-model="form.bankname" placeholder="请输入银行名称" />
                    </el-form-item>
                    <el-form-item label="付款账号" prop="payaccount">
                        <el-input :disabled="disableds" v-model="form.payaccount" placeholder="请输入付款账号" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="19" />
                    </el-form-item>
                </div>
                <el-form-item label="付款凭证">
                    <!-- <el-input
                        v-model="form.payfile"
                        placeholder="请输入付款凭证"
                    /> -->

                    <el-upload :disabled="disableds" :limit="3" ref="legalIdcardFile" :headers="headers" :on-success="idcardUploadSuccess" :file-list="legalIdcardFile" :action="uploadImgUrl" :before-upload="fieldBeforeUpload" :on-remove="idcardRemove" list-type="picture-card">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-show="isshow">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listUplog, getUplog, delUplog, addUplog, updateUplog, exportUplog } from '@/api/biz/uplog';
import { getToken } from '@/utils/auth';

export default {
    name: 'Uplog',
    data() {
        return {
            userinfo: {},
            disabled: true,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 充值申请表格数据
            uplogList: [],
            paytypeOptions: [],
            paycategoryOptions: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                uid: undefined,
                cname: undefined,
                paytime: undefined,
                paytype: undefined,
                payrbtype: undefined,
                payfee: undefined,
                bankname: undefined,
                payaccount: undefined,
                payfile: undefined,
                checkstatus: undefined,
                createdTime: undefined,
                status: undefined
            },
            // 表单参数
            form: {
                id: '',
                uid: '',
                cname: '',
                paytime: '',
                paytype: '',
                payrbtype: '',
                payfee: '',
                bankname: '',
                payaccount: '',
                payfile: '',
                checkstatus: '',
                createdTime: '',
                status: ''
            },
            // 表单校验
            rules: {
                paytime: [
                    {
                        required: true,
                        message: '请选择时间',
                        trigger: 'change'
                    }
                ],
                paytype: [
                    {
                        required: true,
                        message: '请选择汇款方式',
                        trigger: 'change'
                    }
                ],
                payrbtype: [
                    {
                        required: true,
                        message: '请选择付款币种类型',
                        trigger: 'change'
                    }
                ],
                payfee: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '付款金额不能为空'
                    }
                ],
                bankname: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '银行名称不能为空'
                    }
                ],
                payaccount: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '付款账号不能为空'
                    }
                ]
            },
            paytypeArr: [],
            paycategoryArr: [],
            value1: '',
            value2: '',
            options: [
                {
                    value: '0',
                    label: '待审核'
                },
                {
                    value: '1',
                    label: '审核通过，待复审'
                },
                {
                    value: '2',
                    label: '审核不通过'
                },
                {
                    value: '3',
                    label: '复审通过'
                },
                {
                    value: '4',
                    label: '复审不通过'
                }
            ],
            disableds: false,
            // 上传
            legalIdcardFile: [],
            uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
            headers: {
                Authorization: 'Bearer ' + getToken()
            },
            isshow: true,
            fileshost: process.env.VUE_APP_BASE_API
        };
    },
    created() {
        this.getUrl('/web/getCurrectUserinfo').then(response => {
            this.userinfo = response.data;
        });


        this.getDicts('pay_type').then(response => {
            this.paytypeOptions = response.data;
            response.data.forEach((item, index) => {
                this.paytypeArr[item.dictValue] = item.dictLabel;
            });
        });
        this.getDicts('pay_category').then(response => {
            this.paycategoryOptions = response.data;
            response.data.forEach((item, index) => {
                this.paycategoryArr[item.dictValue] = item.dictLabel;
            });
        });

        this.getList();

    },
    methods: {
        //弹窗关闭
        closeDialog() {
            this.legalIdcardFile = [];
        },
        idcardUploadSuccess(response) {
            // console.log(response)
            if (response.code == 200) {
                // this.form.payfile = response.fileName;
                this.legalIdcardFile.push(response.fileName);
            }
        },
        fieldBeforeUpload(file) {
            let isRightSize = file.size / 1024 / 1024 < 2;
            if (!isRightSize) {
                this.$message.error('文件大小超过 2MB');
            }
            return isRightSize;
        },
        idcardRemove(obj) {
            var tmpimg = obj.url.substr(8);
            this.legalIdcardFile.forEach((item, index) => {
                if (item == tmpimg) {
                    this.legalIdcardFile.splice(index, 1);
                }
            });
            // this.legalIdcardFile = undefined
        },
        fileChange() {
            // console.log(this.legalIdcardFile);
        },
        /** 查询充值申请列表 */
        getList() {
            this.loading = true;
            listUplog(this.queryParams, 0).then(response => {
                this.uplogList = response.rows;

                this.uplogList.forEach((item, index) => {
                    if (item.checkstatus == 0) {
                        this.uplogList[index].checkstatus = '待审核';
                    } else if (item.checkstatus == 1) {
                        this.uplogList[index].checkstatus = '审核通过,等待复审';
                    } else if (item.checkstatus == 2) {
                        this.uplogList[index].checkstatus = '审核不通过';
                    } else if (item.checkstatus == 3) {
                        this.uplogList[index].checkstatus = '复审通过';
                    } else if (item.checkstatus == 4) {
                        this.uplogList[index].checkstatus = '复审不通过';
                    }

                    this.uplogList[index].paytype = this.paytypeArr[item.paytype];
                    this.uplogList[index].payrbtype = this.paycategoryArr[item.payrbtype];
                });

                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                uid: undefined,
                cname: undefined,
                paytime: undefined,
                paytype: undefined,
                payrbtype: undefined,
                payfee: undefined,
                bankname: undefined,
                payaccount: undefined,
                payfile: undefined,
                checkstatus: '0',
                createdTime: undefined,
                status: '0'
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
            this.queryParams.checkstatus = '';
            this.queryParams.startTime = '';
            this.queryParams.endTime = '';
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加充值申请';
            this.disableds = false;
            this.isshow = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.legalIdcardFile = [];
            this.reset();
            const id = row.id || this.ids;
            getUplog(id).then(response => {
                this.form = response.data;
                if (this.form.payfile != undefined) {
                    let filesList = this.form.payfile.split(',');
                    filesList.forEach((item, index) => {
                        this.legalIdcardFile.push({
                            name: 'cdsc',
                            url: this.fileshost + item
                        });
                    });
                }
                // this.form.paytype = Number(this.form.paytype);
                // this.form.paybtype = Number(this.form.paybtype);

                this.form.paytype = this.paytypeArr[this.form.paytype];
                this.form.payrbtype = this.paycategoryArr[this.form.payrbtype];

                this.isshow = false;
                this.disableds = true;
                this.open = true;
                this.title = '充值详情';
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            var numReg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
            var numRe = new RegExp(numReg)
            if (!numRe.test(this.form.payfee)) {
                this.msgError("请输入正常格式的金额");
                return false;
            }
            this.form.payfile = this.legalIdcardFile.join(',');
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateUplog(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('修改成功');
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addUplog(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('新增成功');
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除充值申请编号为"' + ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return delUplog(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess('删除成功');
                })
                .catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出充值申请数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return exportUplog(queryParams);
                })
                .then(response => {
                    //this.download(response.msg);
                    this.msgSuccess(response.msg);
                })
                .catch(function() {});
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.widbig input {
    width: 265px;
}
</style>
